package com.xx.happychatboot.middleware.springsecurity.security;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义记住我service 实现类  --- 使用JWT可以不用这个
 */
public class MyPersistentTokenBasedRememberMeServices
        extends PersistentTokenBasedRememberMeServices {
    public MyPersistentTokenBasedRememberMeServices(String key,
                                                    UserDetailsService userDetailsService,
                                                    PersistentTokenRepository tokenRepository) {
        super(key, userDetailsService, tokenRepository);
    }

    /**
     * 自定义前后端分离获取remember-me方式
     *
     * @param request
     * @param parameter
     * @return
     */
    @Override
    protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
        String param = request.getParameter(parameter);
        if (StringUtils.isNotBlank(param)) {
            if (param.equalsIgnoreCase("true")
                    || param.equalsIgnoreCase("yes")
                    || param.equalsIgnoreCase("on")
                    || param.equalsIgnoreCase("1")) {
                return true;
            }
        }
        return false;
    }
}
